package nmrworkflow.framework.gui;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import nmrworkflow.framework.configuration.Preferences;
import nmrworkflow.framework.gui.FileChooser.Type;

public class MainWindowOld extends JFrame
{	
	private FileChooser input, output;
	
	public MainWindowOld()
	{
		super("Nmr Workflow");
		
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
		
		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
		setJMenuBar(createMenuBar());
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		
		panel.add(fileChoosers());
		panel.add(spacer());
		panel.add(workflow());
		panel.add(spacer());
		panel.add(execute());
		panel.add(spacer());
		panel.add(buttons());
		
		this.add(panel);
		pack();
		setVisible(true);
	}
	
	private JPanel fileChoosers()
	{
		JPanel container = createJPanel();
		input = new FileChooser("Input", Type.File, new File("./"));
		output = new FileChooser("Output", Type.File, new File("./"));
		JLabel label = new JLabel("1) Choose your input and output files.");
		
		label.setAlignmentX(JPanel.LEFT_ALIGNMENT);
		input.setAlignmentX(JPanel.LEFT_ALIGNMENT);
		output.setAlignmentX(JPanel.LEFT_ALIGNMENT);
		
		container.add(label);
		container.add(input);
		container.add(output);
		
		return container;
	}
	
	private JPanel workflow()
	{
		JPanel container = createJPanel();
		JButton configure = new JButton("Configure");
		JLabel label = new JLabel("2) Configure the data processing schema.");
		
		label.setAlignmentX(JPanel.LEFT_ALIGNMENT);
		configure.setAlignmentX(JPanel.LEFT_ALIGNMENT);
		
		container.add(label);
		container.add(configure);
		
		return container;
	}
	
	private JPanel execute()
	{
		JPanel container = createJPanel();
		JButton configure = new JButton("Execute");
		JLabel label = new JLabel("3) Execute the data processing schema.");
		
		container.add(label);
		container.add(configure);
		
		
		return container;
	}
	
	private JPanel buttons()
	{
		JPanel container = createJPanel();
		JButton preferences = new JButton("Preferences");
		JButton quit = new JButton("Quit");
		
		quit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		
		preferences.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				PreferencesWindow p = new PreferencesWindow(Preferences.getInstance());
				p.setVisible(true);
			}
		});
		
		// HACK: right align components
		container.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		
		container.setLayout(new BoxLayout(container, BoxLayout.LINE_AXIS));
		container.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
		container.add(quit);
		container.add(preferences);	
		
		return container;
	}
	
	private JPanel spacer()
	{
		JPanel spacer = new JPanel();
		spacer.setPreferredSize(new Dimension(100, 50));
		
		return spacer;
	}
	
	private JMenuBar createMenuBar()
	{
		JMenuBar menuBar = new JMenuBar();
		JMenu file = new JMenu("File");
		JMenu help = new JMenu("Help");
		JMenuItem preferences = new JMenuItem("Preferences...");
		JMenuItem quit = new JMenuItem("Quit");
		JMenuItem getHelp = new JMenuItem("Get Help");
		JMenuItem about = new JMenuItem("About");
		
		preferences.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				PreferencesWindow p = new PreferencesWindow(Preferences.getInstance());
				p.setVisible(true);
			}
		});
		
		quit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		
		file.add(preferences);
		file.add(quit);
		
		help.add(getHelp);
		help.add(about);
		
		menuBar.add(file);
		menuBar.add(help);
		
		return menuBar;
	}
	
	private static JPanel createJPanel()
	{
		JPanel container = new JPanel();
		
		// Center alignment and max size ensures filled
		container.setAlignmentX(JPanel.CENTER_ALIGNMENT);
		container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
		container.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
		
		return container;
		
	}
	
	public static void main(String[] args)
	{	
		MainWindowOld window = new MainWindowOld();
		
	}
	
}
